<template>
	<view style="padding-top: 20upx;">
		
		<view style="height: 160upx;line-height: 160upx;position: relative;background-color: #FFFFFF;"  @click="chooseImage">
			<text style="padding-left: 20upx;font-size: 30upx;color: #666;">修改头像</text>
			<view style="position: absolute;top: 20upx;right: 20upx;border-radius: 60upx;">
				<image :src="avatar || '../../../../static/missing-face.png'" style="width: 120upx;height: 120upx;"></image>
			</view>
		</view>
		
		<!-- <view class="webkit list">
			<view class="title">修改昵称</view>
			<input type="text" :value="nickname" @blur="nicknameIpt" class="flex" />
		</view> -->
		<view style="padding: 0 24upx;">
			<button type="primary" style="background: #EB2E15;margin-top: 20upx;" @click="submit">确认修改</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				nickname:'',
				avatar:'',
				openid:'',
				imagess:[],
				imgurl:'',
			};
		},
		onLoad:function(){
			uni.showLoading()
			uni.getStorage({
				key: 'openid',
				success: (res) => {
					this.openid = res.data
					this.setAjax()
				},
				fail: () => {
					uni.navigateTo({
						url: '../../../login/login'
					});
				}
			});
		},methods:{
			setAjax(){
				uni.request({
					url:'http://hszj.hongshantang696.com//app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=member.face&openid='+this.openid+'&app=1',
					success: res => {
						uni.hideLoading()
						this.nickname = res.data.member.nickname
						this.avatar = res.data.member.avatar
					},
					fail: () => {},
					complete: () => {}
				});
			},
			nicknameIpt(e){
				this.nickname = e.detail.value
			},
			submit(){
				uni.showLoading()
				var data = {
					'openid':this.openid,
					'nickname':this.nickname,
					'avatar':this.avatar,
				};
				uni.request({
					url: 'http://hszj.hongshantang696.com//app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=member.face&app=1',
					method: 'POST',
					data:data,
					header:{'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						uni.hideLoading()
						if(res.data.status ==1){
							uni.showToast({
								title: '修改成功'
							});
							setTimeout(() =>{
								uni.reLaunch({
									url: '../../../../pages/member/member'
								})
							},1200)
						}else{
							uni.showToast({
								title: res.data.result.message,
								icon:"none"
							});
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			chooseImage(){
				var _this = this
				uni.chooseImage({
				    count: 1, //默认9
				    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				    sourceType: ['album'], //从相册选择
				    success: function (res) {
							let file = res.tempFilePaths
							var images = []
							var img = ''
							for(var i = 0; i<res.tempFilePaths.length; i++) {
								uni.uploadFile({
									url: 'http://hszj.hongshantang696.com//app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=util.uploader&app=1',
									//仅为示例，非真实的接口地址
									filePath: file[i],
									name: 'file',
									formData: {
										'user': 'test'
									},
									success: (res) => {
										images.push(JSON.parse(res.data).url)
										_this.imagess = images
										_this.avatar = _this.imagess[0]
										
										img = img + JSON.parse(res.data).url + ';'
										_this.imgurl = img
									}
								});
							}
				    }
				});
			},
		}
	}
</script>

<style>.list{padding:16upx 24upx;background:#fff;}.list .title{width:130upx;line-height:50upx;color:#666;}.list .flex{line-height:50upx;color:#666;padding-left:24upx;}.list .right{color:#999;}</style>